package org.cid54.jshopper.controller.csv;

import java.util.List;
import org.cid54.jshopper.controller.csv.util.GuidChecker;
import org.cid54.jshopper.model.Item;
import org.cid54.jshopper.service.ShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("csv/{userGuid}/items")
public class CsvItemsController {

    @Autowired
    private GuidChecker guidChecker;
    @Autowired
    private ShopItemService shopItemService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String doGetAll(@PathVariable("userGuid") String userGuid) {

        if (this.guidChecker.checkGuid(userGuid)) {

            List<Item> items = this.shopItemService.getItems();

            StringBuilder sb = new StringBuilder();

            for (Item item : items) {

                sb.append(item.getName()).append("\n");

            }

            return sb.toString();

        } else {
            return "";
        }

    }

    @RequestMapping(method = RequestMethod.POST, value = "{shopItemId}/toggle")
    public ResponseEntity<String> doToggleBought(@PathVariable("userGuid") String userGuid, @PathVariable("shopItemId") long shopItemId) {

        if (this.guidChecker.checkGuid(userGuid)) {

            this.shopItemService.toggleBoughtStatus(shopItemId);

            return new ResponseEntity<String>(HttpStatus.OK);

        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }

    }

    @RequestMapping(method = RequestMethod.POST, value = "{shopItemId}/delete")
    public ResponseEntity<String> doDelete(@PathVariable("userGuid") String userGuid, @PathVariable("shopItemId") long shopItemId) {

        if (this.guidChecker.checkGuid(userGuid)) {

            this.shopItemService.deleteShopItem(shopItemId);

            return new ResponseEntity<String>(HttpStatus.OK);

        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }

    }

    @RequestMapping(method = RequestMethod.POST, value = "add")
    public ResponseEntity<String> doAdd(@PathVariable("userGuid") String userGuid, @RequestParam("name") String itemName) {

        if (this.guidChecker.checkGuid(userGuid)) {

            Item item = new Item();

            item.setName(itemName);

            this.shopItemService.createItem(item);

            return new ResponseEntity<String>(HttpStatus.OK);

        } else {
            return new ResponseEntity<String>(HttpStatus.UNAUTHORIZED);
        }

    }
}
